//
//  CAOrderListTableViewCell.m
//  LNH
//
//  Created by twentylx on 2018/2/14.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAOrderListTableViewCell.h"

@implementation CAOrderListTableViewCell

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (!self) return nil;
    
    [self setBackgroundColor:[UIColor whiteColor]];
    
    [self addSubview:self.headView];
    [self.headView addSubview:self.iconImageView];
    [self.headView addSubview:self.orderNoLabel];
    [self.headView addSubview:self.copyButton];
    [self.headView addSubview:self.orderStatusLabel];
    [self addSubview:self.goodsView];
    [self addSubview:self.totalLabel];
    [self addSubview:self.line2View];
    [self addSubview:self.cell1Button];
    [self addSubview:self.cell2Button];
    [self addSubview:self.lastView];
    
    [self layoutPageSubviews];
    
    
    return self;
}

#pragma mark getter and setter
- (UIView *) headView {
    if (!_headView) {
        _headView = [UIView new];
        _headView.backgroundColor = RGBCOLOR(247, 247, 247);
    }
    return _headView;
}

- (UIImageView *) iconImageView {
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
        _iconImageView.image = [UIImage imageNamed:@"orderListIcon"];
    }
    return _iconImageView;
}

- (UILabel *) orderNoLabel {
    if (!_orderNoLabel) {
        _orderNoLabel = [UILabel new];
        _orderNoLabel.textColor = RGBCOLOR(117, 117, 117);
        _orderNoLabel.text = @"订单号：20151212000";
        _orderNoLabel.font = [UIFont systemFontOfSize:14];
    }
    return _orderNoLabel;
}

- (UIButton *) copyButton {
    if (!_copyButton) {
        _copyButton = [UIButton new];
        [_copyButton setTitle:@"复制" forState:UIControlStateNormal];
        [_copyButton setTitleColor:RGBCOLOR(210, 210, 210) forState:UIControlStateNormal];
        _copyButton.titleLabel.font = [UIFont systemFontOfSize:12];
        _copyButton.layer.borderWidth = 1;
        _copyButton.layer.borderColor = [RGBCOLOR(210, 210, 210) CGColor];
        _copyButton.layer.cornerRadius = 2;
    }
    return _copyButton;
}

- (UILabel *) orderStatusLabel {
    if (!_orderStatusLabel) {
        _orderStatusLabel = [UILabel new];
        //        _orderStatusLabel.textColor = RGB(117, 117, 117, 1);
        _orderStatusLabel.text = @"代付款";
        _orderStatusLabel.font = [UIFont systemFontOfSize:14];
        _orderStatusLabel.textAlignment = NSTextAlignmentRight;
        _orderStatusLabel.textColor = RGBCOLOR(235, 109, 52);
        
    }
    return _orderStatusLabel;
}

- (UIView *) goodsView {
    if (!_goodsView) {
        _goodsView = [UIView new];
        _goodsView.backgroundColor = [UIColor clearColor];
        _goodsView.userInteractionEnabled = YES;
    }
    return _goodsView;
}

- (UILabel *) totalLabel {
    if (!_totalLabel) {
        _totalLabel = [UILabel new];
        _totalLabel.font = [UIFont systemFontOfSize:14];
        _totalLabel.textAlignment = NSTextAlignmentRight;
    }
    return _totalLabel;
}

- (UIView *) line2View {
    if (!_line2View) {
        _line2View = [UIView new];
        _line2View.backgroundColor = RGBCOLOR(212, 212, 212);
    }
    return _line2View;
}

- (UIButton *) cell1Button {
    if (!_cell1Button) {
        _cell1Button = [UIButton new];
        [_cell1Button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _cell1Button.layer.borderColor = [RGBCOLOR(212, 212, 212) CGColor];
        _cell1Button.layer.borderWidth = 1;
        _cell1Button.layer.cornerRadius = 5;
        _cell1Button.titleLabel.font = [UIFont systemFontOfSize:12];
    }
    return _cell1Button;
}

- (UIButton *) cell2Button {
    if (!_cell2Button) {
        _cell2Button = [UIButton new];
        [_cell2Button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _cell2Button.layer.borderColor = [RGBCOLOR(212, 212, 212) CGColor];
        _cell2Button.layer.borderWidth = 1;
        _cell2Button.layer.cornerRadius = 5;
        _cell2Button.titleLabel.font = [UIFont systemFontOfSize:12];
    }
    return _cell2Button;
}

- (UIView *) lastView {
    if (!_lastView) {
        _lastView = [UIView new];
        _lastView.backgroundColor = RGBCOLOR(241, 241, 241);
    }
    return _lastView;
}

#pragma mark private method
- (void) configureForCell:(NSDictionary *) model {
    for (UIView *view in [self.contentView subviews]) {
        [view removeFromSuperview];
    }
    __block UIView *lastView = nil;
    NSArray *goodsArray = model[@"order_goods"];
    [goodsArray enumerateObjectsUsingBlock:^(NSDictionary *goodsModel, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIView *goodsBackgroundView = [[UIView alloc] init];
        goodsBackgroundView.backgroundColor = [UIColor whiteColor];
        [self.goodsView addSubview:goodsBackgroundView];
        
        UIImageView *goodsImageView = [UIImageView new];
        [self addSubview:goodsImageView];
        [goodsImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", BASE_IMAGE_DOMAIN, goodsModel[@"goods_img"]]]];
        
        UILabel *goodsNameLabel = [UILabel new];
        goodsNameLabel.numberOfLines = 3;
        goodsNameLabel.lineBreakMode = NSLineBreakByCharWrapping;
        goodsNameLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:goodsNameLabel];
        goodsNameLabel.text = goodsModel[@"goods_name"];
        
        UILabel *countLabel = [UILabel new];
        [self addSubview:countLabel];
        countLabel.textColor = RGBCOLOR(179, 179, 179);
        countLabel.font = [UIFont systemFontOfSize:16];
        countLabel.textAlignment = NSTextAlignmentRight;
        countLabel.text = [NSString stringWithFormat:@"x%@", goodsModel[@"goods_number"]];
        
        
        UILabel *brandLabel = [UILabel new];
        [self addSubview:brandLabel];
        brandLabel.textColor = RGBCOLOR(116, 116, 116);
        brandLabel.font = [UIFont systemFontOfSize:14];
        brandLabel.numberOfLines = 2;
        brandLabel.lineBreakMode = NSLineBreakByCharWrapping;
        
        NSArray *attrArray = goodsModel[@"attr"];
        __block NSString *attrStr = @"";
        [attrArray enumerateObjectsUsingBlock:^(NSDictionary *attrDict, NSUInteger idx, BOOL * _Nonnull stop) {
            attrStr = [NSString stringWithFormat:@"%@ %@:%@", attrStr, attrDict[@"name"], attrDict[@"attr_value"]];
        }];
        brandLabel.text = attrStr;
        
        UIView *lineView = [UIView new];
        [self addSubview:lineView];
        lineView.backgroundColor = RGBCOLOR(224, 224, 224);
        
        
        [goodsBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self);
            if (idx == 0) {
                make.top.equalTo(self.headView.mas_bottom);
            } else {
                make.top.equalTo(lastView.mas_bottom);
            }
            make.height.mas_equalTo(120);
        }];
        
        [goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(goodsBackgroundView).offset(10);
            make.top.equalTo(goodsBackgroundView).offset(10);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(100);
        }];
        
        [goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(goodsBackgroundView).offset(115);
            make.top.equalTo(goodsImageView).offset(5);
            make.right.equalTo(countLabel.mas_left);
        }];
        
        [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(goodsImageView);
            make.right.equalTo(goodsBackgroundView).offset(-20);
            make.width.mas_equalTo(40);
        }];
        
        [brandLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(goodsNameLabel);
            make.top.equalTo(goodsNameLabel.mas_bottom).offset(10);
            make.right.equalTo(countLabel.mas_left);
        }];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(goodsBackgroundView);
            make.bottom.equalTo(goodsBackgroundView);
            make.height.mas_equalTo(1);
        }];
        
        lastView = goodsBackgroundView;
    }];
    
    _orderNoLabel.text = [NSString stringWithFormat:@"订单编号：%@", model[@"order_sn"]];;
    _orderStatusLabel.text = model[@"status_text"];
    //    [self checkStatusAndSetButton:model.status];
    _totalLabel.text = [NSString stringWithFormat:@"共%@件商品  合计：%@", model[@"order_goods_number"], model[@"order_price"]];
    
    [_goodsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self);
        make.top.equalTo(self.headView.mas_bottom);
        make.bottom.equalTo(self.line2View.mas_top);
    }];
    
    [_totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(lastView.mas_bottom).offset(10);
        make.right.equalTo(self).offset(-15);
    }];
    
    [_line2View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self);
        make.top.equalTo(self.totalLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(1);
    }];
    
    [_cell1Button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line2View.mas_bottom).offset(20);
        make.right.equalTo(self.cell2Button.mas_left).offset(-20);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(30);
    }];
    
    [_cell2Button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line2View.mas_bottom).offset(20);
        make.right.equalTo(self).offset(-20);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(30);
    }];
    
    [_lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self);
        make.bottom.equalTo(self);
//        make.height.mas_equalTo(20);
    }];
    
}

#pragma mark constraints
- (void) layoutPageSubviews {
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self);
        make.top.equalTo(self);
        make.height.mas_equalTo(44);
    }];
    
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.centerY.equalTo(self.headView);
    }];
    
    [_orderNoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(5);
        make.centerY.equalTo(self.headView);
    }];
    
    [_copyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.orderNoLabel.mas_right).offset(5);
        make.centerY.equalTo(self.headView);
        make.height.mas_equalTo(12);
    }];
    
    [_orderStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.headView);
        make.right.equalTo(self).offset(-15);
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
